<?php

namespace Single\Logic;

use School\PBClass;
use School\PBStudent;
use School\PBStudentDegree;
use School\PBStudentListReq;
use School\PBStudentListRsp;
use School\PBUser;

class StudentList extends Base
{
    public $limit;
    public $offset;

    /**
     * StudentList constructor.
     * @param PBStudentListReq $req
     */
    public function __construct(PBStudentListReq $req)
    {
        $this->limit = $req->getLimit();
        $this->offset = $req->getOffset();
    }

    public function run(): \Google\Protobuf\Internal\Message
    {
        $list = [];

        for ($i = 0; $i < $this->limit; $i++) {
            $id = $this->offset + $i;
            $Student = (new PBStudent())->setStudentId(1)
                ->setClass(
                    (new PBClass([
                        'classId' => $i, 'name' => $id . 'name'
                    ]))
                )
                ->setUser(
                    (new PBUser([
                        'user_id' => $id, 'name' => $id . 'name' . $id, 'mobile' => time() + $id
                    ]))
                )
                ->setDegree(PBStudentDegree::MIDDLE);
            $list[] = $Student;
        }

        $rsp = new PBStudentListRsp();
        $rsp->setList($list);
        return $rsp;
    }
}
